<template>
  <div class="register-page">
    <el-card>
        <el-form :model="form" :rules="rules" ref="form">
            <el-form-item>
                <h2 class="title">后台管理系统</h2>
                <router-link to="/login" class="register">去登录</router-link>
            </el-form-item>
            <el-form-item label="用户名" prop="username">
                <el-input placeholder="请输入用户名" v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" placeholder="请输入密码" v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="repassword">
                <el-input type="password" placeholder="请再次输入密码" v-model="form.repassword"></el-input>
            </el-form-item>
            <el-form-item><el-button class="login-button" @click="submit">注册</el-button></el-form-item>
        </el-form>
</el-card>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  name: 'register-page',
  data () {
    return {
      form: {
        username: '',
        password: '',
        repassword: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入 用户名', trigger: ['blur', 'change'] },
          { min: 5, max: 11, message: '长度在5-11个字符' }
        ],
        password: [
          { required: true, message: '请输入 密码', trigger: ['blur', 'change'] },
          // 正则校验
          { pattern: /^\w{5,11}$/, message: '长度在5-11个字符', trigger: ['blur', 'change'] }
        ],
        repassword: [
          { required: true, message: '请确认 密码', trigger: ['blur', 'change'] },
          // 正则校验
          { pattern: /^\w{5,11}$/, message: '长度在5-11个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    ...mapActions('user', ['registerEvent']),
    async submit () {
      await this.$refs.form.validate()
      if (this.form.password !== this.form.repassword) {
        this.$message.error('两次密码不一致')
        return
      }
      await this.registerEvent(this.form)
      this.$message.success('注册成功')
      this.reset()
      this.$router.push('/login')
    },
    reset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.register-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 540px;
    .el-card__header{
      text-align: center;
      line-height: 40px;
      color: black;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
    .title{
        font-size: 32px;
        color: #131523;
        text-align: center;
        line-height: 44px;
        font-weight: 400;
    }
    .register {
        display: block;
        text-decoration: none;
        font-size: 16px;
        color: #1E5EFF;
        text-align: center;
        line-height: 24px;
        font-weight: 400;
    }
  }
  .tc {
    text-align: center;
  }
  .login-button {
    width: 100%;
    height: 48px;
    background: #4F81FF;
    border-radius: 4px;
    color: #fff;
    font-size: 16px;
}
}
</style>
